route.test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // app/api/branches/[branch]/[year]/[month]/days/route.test.js
  2. import { describe, it, expect, beforeAll, afterAll } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. import { GET as getDays } from "./route.js";
  7. let tmpRoot;
  8. beforeAll(async () => {
  9. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-days-"));
  10. process.env.NAS_ROOT_PATH = tmpRoot;
  11. // tmpRoot/NL01/2024/10/23
  12. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  13. recursive: true,
  14. });
  15. });
  16. afterAll(async () => {
  17. await fs.rm(tmpRoot, { recursive: true, force: true });
  18. });
  19. describe("GET /api/branches/[branch]/[year]/[month]/days", () => {
  20. it("returns days for a valid branch/year/month", async () => {
  21. const req = new Request("http://localhost/api/branches/NL01/2024/10/days");
  22. const ctx = {
  23. params: Promise.resolve({
  24. branch: "NL01",
  25. year: "2024",
  26. month: "10",
  27. }),
  28. };
  29. const res = await getDays(req, ctx);
  30. expect(res.status).toBe(200);
  31. const body = await res.json();
  32. expect(body).toEqual({
  33. branch: "NL01",
  34. year: "2024",
  35. month: "10",
  36. days: ["23"],
  37. });
  38. });
  39. it("returns 400 when any param is missing", async () => {
  40. const req = new Request("http://localhost/api/branches/NL01/2024/10/days");
  41. const ctx = {
  42. params: Promise.resolve({ branch: "NL01", year: "2024" }), // month missing
  43. };
  44. const res = await getDays(req, ctx);
  45. expect(res.status).toBe(400);
  46. const body = await res.json();
  47. expect(body.error).toBe("branch, year oder month fehlt");
  48. });
  49. });